package com.ibilibili.crawl;


import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class CrawlThread extends Thread {
    private int endTid = 0;
    private int beginTid = 0;
    private int beginPn = 0;
    private int endPn = 0;

    public CrawlThread(int beginTid, int endTid, int beginPn, int endPn) {
        this.beginTid = beginTid;
        this.endTid = endTid;
        this.beginPn = beginPn;
        this.endPn = endPn;
    }

    public void run() {
        int tid = this.beginTid;
        while (true) {
            if (tid == this.endTid) {
                break;
            }
            try {
                // tmp test
                if (tid == 0 || tid == 15 || tid == 16 || tid == 17 || tid ==18) {
                    continue;
                }
                System.out.println("beginTid: " + this.beginTid + " ,endTid: " + this.endTid + " ,beginPn: " + this.beginPn + " ,endPn: " + this.endPn);

                for (int pn = this.beginPn; pn <= this.endPn; pn++) {
                    HttpClient httpClient = HttpClients.createDefault();
                    System.out.println("tid= " + tid + ", pn=" + pn);
                    String requestUrl = "http://api.bilibili.com/archive_rank/getarchiverankbypartion?callback=?&type=json&tid=" + tid + "&pn=" + pn;
                    HttpGet req = new HttpGet(requestUrl);
                    req.addHeader("Accept", "application/json, text/javascript, */*; q=0.01");
                    req.addHeader("Accept-Encoding", "gzip,deflate,sdch");
                    req.addHeader("Accept-Language", "zh-CN,zh;q=0.8");
                    req.addHeader("Content-Type", "textml; charset=UTF-8");
                    req.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36");
                    HttpResponse rep = httpClient.execute(req);
                    HttpEntity repEntity = rep.getEntity();
                    String jsonMessage = EntityUtils.toString(repEntity);
                    JSONDeal jd = new JSONDeal(jsonMessage);
                    jd.parse();
                    Thread.sleep(50);
                    System.out.println("tid: " + tid + ", pn: " + pn);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            tid ++;
        }
    }
}
